//
//  LEAlertViewController.m
//  LESports
//
//  Created by 金玉龙 on 15/12/16.
//  Copyright © 2015年 LETV. All rights reserved.
//

#import "LEAlertViewController.h"

#define clearColor [UIColor clearColor]

@interface LEAlertViewController ()

@end

@implementation LEAlertViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCompnent];
    [self layoutCompnent];
    [self loadButtons];
}

- (void)initCompnent{
    self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.view addSubview:self.bgImageView];
    [self.view addSubview:self.blurView];
    [self.view addSubview:self.messageBox];
    [self.messageBox addSubview:self.titleLabel];
    [self.messageBox addSubview:self.messageLabel];
    [self.messageBox addSubview:self.separateLine];
}

- (void)layoutCompnent{
    WS(weakSelf)
    [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    [_blurView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    [_messageBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(250, 125));
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_messageBox).offset(8);
        make.left.equalTo(_messageBox).offset(35);
        make.right.equalTo(_messageBox).offset(-35);
        make.height.mas_equalTo(28);
    }];
    [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_messageBox).offset(10);
        make.right.equalTo(_messageBox).offset(-10);
        make.top.equalTo(_titleLabel.mas_bottom).offset(8);
        make.bottom.equalTo(_separateLine.mas_top).offset(-8);
    }];
    [_separateLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_messageBox).offset(20);
        make.right.equalTo(_messageBox).offset(-20);
        make.bottom.equalTo(_messageBox).offset(-41);
        make.height.mas_equalTo(0.5);
    }];
}
//截图做背景
- (void)getBackgroundImageFromCtrl:(UIViewController*)ctrl{
    UIGraphicsBeginImageContext([UIScreen mainScreen].bounds.size);
    [ctrl.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView *tempImgView = [[UIImageView alloc] initWithImage:image];
    self.bgImageView = tempImgView;
}

- (void)loadButtons{
    [self.messageBox addSubview:self.confirmButton];
    [_confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(_messageBox);
        make.top.equalTo(_separateLine.mas_bottom);
    }];
}

- (void)buttonClick:(UIButton*)button{
    if ([_delegate respondsToSelector:@selector(alertButtonClicked:buttonIndex:)]) {
        [_delegate alertButtonClicked:button buttonIndex:1];
    }    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    for (UITouch *touch in touches) {
        if ([touch.view isEqual:self.blurView]) {
            [self dismissViewControllerAnimated:YES completion:nil];
            return;
        }
    }
}

- (UIButton*)buttonWithTitle:(NSString*)title{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundColor:clearColor];
    button.titleLabel.font = kFont(14);
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}
#pragma mark - Setter and Getter
- (UIImageView *)bgImageView{
    if (_bgImageView == nil) {
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.backgroundColor = clearColor;
    }
    return _bgImageView;
}

- (UIView *)blurView{
    if (_blurView == nil) {
        _blurView = [UIView new];
        _blurView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }
    return _blurView;
}

- (UIView *)messageBox{
    if (_messageBox == nil) {
        _messageBox = [UIView new];
        _messageBox.layer.cornerRadius = 8;
        _messageBox.backgroundColor = [UIColor colorWithRed:0.161 green:0.778 blue:0.755 alpha:1.000];
    }
    return _messageBox;
}

-(UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [UILabel new];
        _titleLabel.backgroundColor = clearColor;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.text = @"保存图片被阻止了";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

-(UILabel *)messageLabel{
    if (_messageLabel == nil) {
        _messageLabel = [UILabel new];
        _messageLabel.backgroundColor = clearColor;
        _messageLabel.textColor = [UIColor whiteColor];
        _messageLabel.text = @"请到系统\"设置:隐私>照片\"中开启\"乐视体育\"访问权限";
        _messageLabel.font = kFont(12);
        [_messageLabel adjustsFontSizeToFitWidth];
        _messageLabel.numberOfLines = 2;
    }
    return _messageLabel;
}

-(UIView *)separateLine{
    if (_separateLine == nil) {
        _separateLine = [UIView new];
        _separateLine.backgroundColor = [UIColor whiteColor];
    }
    return _separateLine;
}

- (UIButton *)confirmButton{
    if (_confirmButton == nil) {
        _confirmButton = [self buttonWithTitle:@"好的"];
        _buttenIndex = 0;
    }
    return _confirmButton;
}


- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

@end
